package xs.com.location.utils;

import android.content.Context;

import com.amap.api.location.AMapLocationListener;
import com.amap.api.location.LocationManagerProxy;
import com.amap.api.location.LocationProviderProxy;

/**
 * Created by Administrator on 2015/8/11.
 */
public class LocationManagerTool {

    public static void locationListen(Context context, AMapLocationListener listener) {
        LocationManagerProxy mLocationManagerProxy = LocationManagerProxy.getInstance(context);
        setListener(mLocationManagerProxy, LocationProviderProxy.AMapNetwork, -1, 15, listener);

    }

    public static void setListener(LocationManagerProxy mLocationManagerProxy, String provider, long minTime, float minDistance, AMapLocationListener listener) {


        //此方法为每隔固定时间会发起一次定位请求，为了减少电量消耗或网络流量消耗，
        //注意设置合适的定位时间的间隔，并且在合适时间调用removeUpdates()方法来取消定位请求
        //在定位结束后，在合适的生命周期调用destroy()方法
        //其中如果间隔时间为-1，则定位只定一次
        mLocationManagerProxy.requestLocationData(
                provider, minTime, minDistance, listener);
    }

    public static void stopLocation(Context context, AMapLocationListener listener) {
        LocationManagerProxy mLocationManagerProxy = LocationManagerProxy.getInstance(context);
        if (mLocationManagerProxy != null) {
            mLocationManagerProxy.removeUpdates(listener);
            mLocationManagerProxy.destroy();
        }
    }
}
